package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.BaseEntity;
import com.atguigu.lease.model.entity.SystemPost;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.web.admin.mapper.SystemPostMapper;
import com.atguigu.lease.web.admin.mapper.SystemUserMapper;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserItemVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;

/**
 * @author liubo
 * @description 针对表【system_user(员工信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class SystemUserServiceImpl extends ServiceImpl<SystemUserMapper, SystemUser>
        implements SystemUserService {
@Autowired
private SystemUserMapper systemUserMapper;
@Autowired
private SystemPostMapper systemPostMapper;

    @Override
    public void queryPage(Page<SystemUserItemVo> page, SystemUserQueryVo queryVo) {
         systemUserMapper.queryPage(page,queryVo);
    }

    @Override
    public SystemUserItemVo customGetById(Long id) {
        //1.获取systemUser单表信息,直接调用getById,因为已经在service层了.
        SystemUser systemUser = getById(id);
        //2.获取岗位名称
        LambdaUpdateWrapper<SystemPost> a = new LambdaUpdateWrapper<>();
        a.eq(SystemPost::getId,systemUser.getPostId());
        SystemPost systemPost = systemPostMapper.selectOne(a);
        SystemUserItemVo systemUserItemVo = new SystemUserItemVo();
        systemUserItemVo.setPostName(systemPost.getName());
        BeanUtils.copyProperties(systemUser,systemUserItemVo);
        return systemUserItemVo;
    }

    @Override
    public void customsaveOrUpdate(SystemUser systemUser) {
        //1.判断用户名状态 如果为保存,则数据库不能存在其它相同用户名,如果为更新,则用户名能重复自己,不能重复其他人
        Long id = systemUser.getId();
        LambdaQueryWrapper<SystemUser> a = new LambdaQueryWrapper<>();
        a.eq(SystemUser::getUsername,systemUser.getUsername());
        a.ne(id!=null, SystemUser::getId,systemUser.getId());
        long count = count(a);
        if (count>0){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_EXIST_ERROR);
        }
        //2.密码加密
        if (!ObjectUtils.isEmpty(systemUser.getPassword())){
            //throw new LeaseException("密码不能为空");
            systemUser.setPassword(DigestUtils.md5Hex(systemUser.getPassword()));
        }



        //3.保存或更新
        saveOrUpdate(systemUser);
    }
}




